import com.mulanweb.api.module.model.Lanmao;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/19.
 */
public class ZSWPhoto {

  /*  public static void main(String[] args) throws IOException{
        String home = "http://i.qq.com/?s_url=http%3A%2F%2Fuser.qzone.qq.com%2F1741827765%3Fptlang%3D2052%26t%3D0.10575366964764776";
        Document doc = getDocument(home);
        doc.select("#u").val("1270840060");
        doc.select("#p").val("zenghf(*)521");
        System.out.println(doc);
    }*/


    static List<String> detailPageUl = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        int pageNumber = 1;

        // String home = "https://lmlxhwd.alitrip.com/search.htm?spm=a1z10.1-b-s.w11422135-14856052041.8.CIsouK&search=y&scene=taobao_shop";
        //String home = "https://lmlxhwd.alitrip.com/i/asynSearch.htm?_ksTS=1489398943387_287&callback=jsonp288&mid=w-14844646096-0&wid=14844646096&path=/search.htm&&search=y&spm=a1z10.1-b-s.w11422135-14856052041.8.CIsouK&scene=taobao_shop";
        String home = "https://lmlxhwd.alitrip.com/i/asynSearch.htm?_ksTS=1489401323463_287&callback=jsonp288&mid=w-14844646096-0&wid=14844646096&path=/search.htm&&search=y&spm=a1z10.3-b-s.w4011-14844646096.277.HOmLkG&scene=taobao_shop&pageNo=_pageNumber_&tsearch=y".replace("_pageNumber_", pageNumber + "");
        Document doc = getDocument(home);
        Document dd = new Document(doc.html().replace("\\", "").replace("&quot;", ""));
        while (doc.html() != null && pageNumber<10) {
            Elements elements = doc.select("dl");
            for (Element e : elements) {
                String zong_xiao_liang = e.select("dd").get(0).select("span").get(2).text();
                String url = e.select("a").attr("href").replace("\"", "").replace("\\", "");
                url = url + "_CHANGPIN_ZONG_XIAO_LIANG_="+zong_xiao_liang;
                detailPageUl.add(url);
            }

            pageNumber++;
            home = "https://lmlxhwd.alitrip.com/i/asynSearch.htm?_ksTS=1489401323463_287&callback=jsonp288&mid=w-14844646096-0&wid=14844646096&path=/search.htm&&search=y&spm=a1z10.3-b-s.w4011-14844646096.277.HOmLkG&scene=taobao_shop&pageNo=_pageNumber_&tsearch=y".replace("_pageNumber_", pageNumber + "");
            doc = getDocument(home);
        }
        getDetailInfo();

    }

    public static void getDetailInfo() throws IOException{
        int i = 0 ;
        int f = 0 ;
        for(String url : detailPageUl){
            Document doc = getDocument(url);
            String[] urlName = url.split("_CHANGPIN_ZONG_XIAO_LIANG_=");
            String ur = urlName[0];
            String zongxiaoliang = urlName[1];
            String name = doc.getElementById("J_PropertyWrap").select("h1").get(0).text() ;
            String price = doc.getElementById("J_Price").select("span").get(0).text();
            String yuexiaoliang = doc.getElementById("J_SellCountWrap").select("em").text();

            Lanmao lanmao = new Lanmao();
            lanmao.setName(name);
            lanmao.setPrice(price);
            lanmao.setUrl(url);
            lanmao.setXiaoliang(yuexiaoliang);
            lanmao.setZongxiaoling(zongxiaoliang);
            if(lanmao.save()){
                i++;
                System.out.println("成功保存："+ i +"条产品信息");
            }else{
                f++;
                System.out.println("---失败："+ f +"条产品信息");
            }

        }

    }


    public static Document getDocument(String url) throws IOException {
        return Jsoup.connect(url).timeout(30000).get();
    }

    public void saveFile (Integer pageNumber , Document doc) throws  IOException{
        String filename = "D:/temp/_" + pageNumber + ".html";
        File file = new File(filename);
        if (!file.exists()) {
            file.createNewFile();
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            PrintWriter printWriter = new PrintWriter(fileOutputStream);
            printWriter.write(doc.html().replace("\\", "").replace("&quot;", ""));
            printWriter.flush();

            fileInputStream.close();
            inputStreamReader.close();
            bufferedReader.close();
            fileOutputStream.close();

        }
    }
}
